package com.tianqu.tms.www.car.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.CommercialVendor;
import com.tianqu.tms.entity.Vendor;

/**
 * @author zhangxueliang
 * 载入品牌
 */
public class CarLoadVendorAjaxAction extends TmsAjaxAction{
	private Logger logger = Logger.getLogger(CarLoadVendorAjaxAction.class);
	private static final String FROMTYPE = "fromType";
	private static final String VENDORID="vendorId";
	@Override
	public void process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		controller.begin();
		String fromType = request.getParameter(FROMTYPE);
		String vendorId = request.getParameter(VENDORID);
		// 车辆品牌
		
		
		List list = new ArrayList();
		if("1".equals(fromType)){
			Vendor vendor = Vendor.getInstance();
			 list = vendor.findAllVendorInfo();// 车主类型list
		}else if("2".equals(fromType)){
			CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
			 list = commercialVehicle.findAllVendorInfo();// 车主类型list
		}
		
		//拼select
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"carVendor\"   class=\"semcarchek requirecs\" id=\"carVendor\" onchange=\"selectBrandByCon(this.value);\">");
		Map map = null;
		int j=0;
		for(int i=0;i<list.size();i++)
		{
			if(j==0){
				str.append("<option value=\""+""+"\">"+"品牌"+"</option>");
			}
			map =(Map)list.get(i);
			//设置默认参数
			if(map.get("vendorId").equals(vendorId)){
				str.append("<option value='");
				str.append(map.get("vendorId"));
				str.append("'");
				str.append(" selected='");
				str.append("true'");
				str.append(" >");
				str.append(map.get("vendorName"));
				str.append("</option>");
			
			}else{
				str.append("<option value=\""+map.get("vendorId")+"\">"+map.get("vendorName")+"</option>");
			}
			
			j++;
		}
		str.append("</select>");
		
		String responseText =str.toString();
		controller.commit();
		responseValues(response, responseText, TEXT);
		
		logger.debug(this.getClass().getName() + "#doAction was completed.");
	}

}
